<?php

Class questions_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function getQuestions($story) {
        $data= $this->db->get_where('questions', array('story_id' => $story));
        $question = $data->result_array();
        $instructions = $this->getInstructions($story);
        foreach ($question as $item) {
            $item['choices'] = $this->getChoices($item['question_id']);
            $questions[] = $item;
        }
        $questions['instructions'] = array('type' => 'instructions','content' => $instructions);
        return $questions;
    }

    public function getChoices($question) {
        $this->db->select('ad.answer_id,ad.answer');
        $this->db->from('questions q,answers a, answers_detail ad');
        $this->db->where('q.question_id = a.question_id');
        $this->db->where('a.answer_id = ad.answer_id');
        $this->db->where('q.question_id',$question);
        $data = $this->db->get();
           
        $choices = $data->result_array();

        return $choices;
    }
    
    public function getInstructions($story){
        $data = $this->db->get_where('instructions', array('story_id' => $story));
        $instructions = $data->result_array();
        foreach($instructions as $item){
            $instruction[] = array('description' =>$item['description'],'type' =>$item['type']);
        }
        return $instruction;
    }

}

?>